//
//  YCUpdateNameViewController.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/10.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  更新用户名

#import "YCUpdateNameViewController.h"

@interface YCUpdateNameViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,readonly) UITableView *tableView;
@property (nonatomic,readonly) UITextField *textField;
@property (nonatomic,readonly) NSString *name;

@end

@implementation YCUpdateNameViewController

@synthesize tableView = _tableView;
@synthesize textField = _textField;
@synthesize name = _name;
#pragma mark - 属性获取方法

- (UITableView *)tableView
{
    if(!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableView.bounds.size.width, 23.5)];
    }
    return _tableView;
}

#pragma mark - 视图控制器生命周期

- (instancetype)initWithName:(NSString *)name
{
    if(self = [super init])
    {
        _name = name;
    }
    return self;
}

- (void)dealloc
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(self.title, nil);
    
    [self yc_addLeftBarButtonItemWithTitle:@"取消" target:self action:@selector(didTouchUpInsideCancelBarButtonItem:)];
    [self yc_addRightBarButtonItemWithTitle:@"完成" target:self action:@selector(didTouchUpInsideFinishBarButtonItem:)];
    
    [self layout];
}

#pragma mark - 自定义的方法

- (void)layout
{
    self.view.backgroundColor = [UIColor yc_colorWithUInt:0xefeff4];
    [self.view addSubview:self.tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.top.bottom.equalTo(self.view);
    }];
}

- (void)didTouchUpInsideCancelBarButtonItem:(UIBarButtonItem *)cancelItem
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didTouchUpInsideFinishBarButtonItem:(UIBarButtonItem *)finishItem
{
    [self.view endEditing:YES];
    
    NSString *name = self.textField.text;
    if(name.length < 1)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入姓名"];
        return;
    }
    
    if([name isEqualToString:self.name])//名字未修改,不发请求
    {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    // 发送修改备注的请求
    NSDictionary *ps = @{@"nickName":name};
}

#pragma mark - UITableView delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CELLID = @"CELLID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
    
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELLID];
        
        CGRect rect = cell.contentView.bounds;
        rect.origin.x += 23;
        rect.size.width -= 23;
        
        _textField = [[UITextField alloc] initWithFrame:rect];
        _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _textField.text = self.name;
        _textField.delegate = self;
        _textField.font = [UIFont systemFontOfSize:15];
        _textField.placeholder = @"请输入姓名";
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [cell.contentView addSubview:_textField];
        
    }
    return cell;
}

#pragma mark - UITextField delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
